import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common_ui/src/common_ui_config.dart';
import 'package:flutter_common_ui/src/popup/popup_select.dart';
import 'package:get/get.dart';

import '../other/ink_well_x.dart';

class PopupInputController {
  late VoidCallback _showPopupCallback;

  void bind(VoidCallback showPopup) {
    _showPopupCallback = showPopup;
  }

  void showPopup() {
    _showPopupCallback();
  }
}

class PopupInput extends StatefulWidget {
  final PopupInputController? controller;
  final String title;
  final double width;
  final double height;
  final double popWidth;
  final double popHeight;
  final double? dialogHeight;
  final double dialogMaxHeight;
  final int? maxLines;
  final TextOverflow? overflow;
  final dynamic select;

  //当选择后是否设置成hover样式
  final bool isSelectToHover;
  final BorderRadius? borderRadius;
  final Color? borderUnColor;

  //是否显示InputDecorator
  final bool isInputDecorator;
  final List<dynamic> data;
  final ValueChanged<dynamic>? onChanged;
  final AsyncValueGetter<List<dynamic>?>? onClicked;
  final tag = "popupInput";

  PopupInput({
    super.key,
    this.controller,
    this.title = "",
    required this.select,
    List<dynamic>? data,
    this.width = 150,
    this.height = 40,
    this.popWidth = 150,
    this.popHeight = 40,
    this.dialogHeight,
    this.dialogMaxHeight = 240,
    this.isSelectToHover = false,
    this.isInputDecorator = false,
    this.borderRadius,
    this.borderUnColor,
    this.maxLines,
    this.overflow,
    this.onChanged,
    this.onClicked,
  }) : data = data ?? const [] {}

  @override
  State<StatefulWidget> createState() => _PopupInputState();
}

class _PopupInputState extends State<PopupInput> {
  final isHover = false.obs;
  final _focusNode = FocusNode();

  bool get isSelect => widget.select != null;

  String get selectText => widget.select == null
      ? ""
      : widget.select is IShowText
          ? widget.select.showText()
          : widget.select.toString();

  @override
  void initState() {
    super.initState();
    widget.controller?.bind(() {
      _showPopup();
    });
  }

  @override
  void didUpdateWidget(covariant PopupInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    widget.controller?.bind(() {
      _showPopup();
    });
  }

  void _showPopup() async {
    _focusNode.requestFocus();
    final resultData = await widget.onClicked?.call() ?? widget.data;
    WidgetsBinding.instance.addPostFrameCallback((it) {
      showPopupList<dynamic>(
        context,
        data: resultData,
        selectedItem: widget.select,
        width: widget.popWidth,
        itemHeight: widget.popHeight,
        height: widget.dialogHeight,
        maxLines: widget.maxLines,
        overflow: widget.overflow,
        dialogMaxHeight: widget.dialogMaxHeight,
        // selectedItem: select,
        onChanged: widget.onChanged,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return InkWellX(
      onTap: _showPopup,
      onHover: (value) {
        isHover.value = value;
      },
      borderRadius: widget.borderRadius ?? BorderRadius.circular(10),
      focusColor: Colors.transparent,
      child: _buildContent(context),
    );
  }

  Widget _buildContent(BuildContext context) {
    return Obx(() {
      final color = (widget.isSelectToHover && isSelect) || isHover.value ? Theme.of(context).primaryColor : widget.borderUnColor ?? CommonUiConfig.getPopupDividerColor();
      if (widget.isInputDecorator) {
        return Focus(
          focusNode: _focusNode,
          child: SizedBox(
            width: widget.width,
            height: widget.height,
            child: InputDecorator(
              decoration: InputDecoration(
                labelText: widget.title,
                labelStyle: TextStyle(color: isHover.value ? Theme.of(context).primaryColor : Theme.of(context).textTheme.labelLarge?.color),
                enabledBorder: OutlineInputBorder(borderRadius: widget.borderRadius ?? BorderRadius.circular(10.0), borderSide: BorderSide(color: color, width: 1)),
                border: OutlineInputBorder(borderRadius: widget.borderRadius ?? BorderRadius.circular(10.0), borderSide: BorderSide(color: color, width: 1)),
              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 5),
                child: Center(
                    child: Text(
                  widget.select?.toString() ?? "",
                  maxLines: widget.maxLines,
                  overflow: widget.overflow,
                )),
              ),
            ),
          ),
        );
      }
      return Focus(
        focusNode: _focusNode,
        child: Container(
          width: widget.width,
          height: widget.height,
          decoration: BoxDecoration(borderRadius: widget.borderRadius ?? BorderRadius.circular(10), border: Border.all(color: color, width: 1)),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 5),
            child: Center(
              child: Text(
                widget.select == null ? widget.title : selectText,
                style: TextStyle(color: (widget.isSelectToHover && isSelect) || isHover.value ? Theme.of(context).primaryColor : Theme.of(context).textTheme.labelLarge?.color, fontSize: 14),
                maxLines: widget.maxLines,
                overflow: widget.overflow,
              ),
            ),
          ),
        ),
      );
    });
  }
}
